<?php
class User extends CI_Controller{
    function  __construct(){
        parent::__construct();
        $this->load->library(array('form_validation','session','user_libs'));
        $this->load->helper(array( 'url','form'));
        
        // Kiem tra da dang nhap tu frontend hay chua. 
        // Kiem tra co phai la admin khong
        if(!$this->user_libs->isLogin() || !$this->user_libs->isAdmin()){
            redirect(base_url().'backend/login');
            exit();
        }
    }
    function index(){
        redirect(base_url().'backend/user/listuser');
    }
    function listuser(){
        $this->load->library('pagination');
        $config['base_url'] = base_url().'backend/user/listuser';
        $config['total_rows'] = $this->user_model->numRows(); // Tong so record trong db
        $config['per_page'] = 1; // So record trong mot trang
        $config['uri_segment'] = 4; // vi tri so 4 trong duong link
        $config['num_links'] = 2; // tuc la so trang hien thi ben trai, ben phai trang hien tai
        
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Prev';

        $segment = $this->uri->segment($config['uri_segment']);
        $this->pagination->initialize($config); 
        $data['link'] = $this->pagination->create_links();
        $data['users'] = $this->user_model->getAll($config['per_page'],$segment);
        $data['page'] = 'backend/user/listuser';
        $this->load->view('backend/temp',$data);
    }
    function deluser(){
        $userid = $this->uri->segment(4);
        if(is_numeric($userid)){
            $this->user_model->deleteUser($userid);
            redirect('backend/user/listuser');
        }
        else  echo 'khong hop le';
    }
    function adduser(){
        $data['page'] = 'backend/user/adduser';
        $this->load->view('backend/temp',$data);
    }
    function edituser(){
        
    }
    function logout(){
        $this->session->sess_destroy();
        redirect(base_url().'backend/login'); 
    }
}